package cn.seaboot.admin.core.service;

import cn.seaboot.admin.core.bean.Dictionary;
import cn.seaboot.admin.core.bean.DictionaryConfig;
import cn.seaboot.admin.core.dao.DictionaryConfigDao;
import cn.seaboot.admin.mvc.argument.ModifyArgument;
import cn.seaboot.admin.mvc.valid.ValidationUtils;
import cn.seaboot.commons.core.UUIDUtils;
import cn.seaboot.commons.exception.BizException;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * Service
 * [t_sys_dictionary_config]
 *
 * @author Mr.css
 * @version 2021-12-09 14:51
 */
@Service
public class DictionaryConfigService {

    @Resource
    private DictionaryConfigDao sysDicConfigDao;

    @Resource
    private DictionaryService dictionaryService;

    /**
     * 增
     *
     * @param pojo pojo
     * @return affected rows
     */
    public int insert(DictionaryConfig pojo) {
        if (pojo.getEditable() == null) {
            pojo.setEditable(Boolean.TRUE);
        }
        return sysDicConfigDao.insert(pojo);
    }

    /**
     * 删
     *
     * @param tableName  表名
     * @param columnName 列名
     * @return affected rows
     */
    public int deleteById(String schemaName, String tableName, String columnName) {
        int ret = 0;
        ret += sysDicConfigDao.deleteById(schemaName, tableName, columnName);
        ret += dictionaryService.deleteByTableName(schemaName, tableName, columnName);
        return ret;
    }

    /**
     * 改
     *
     * @param pojo pojo
     * @return affected rows
     */
    public int updateById(DictionaryConfig pojo) {
        return sysDicConfigDao.updateSampleById(pojo);
    }


    /**
     * 快捷更新
     *
     * 像是用户信息这些内容，可以输入之后，立即保存的方式做，这时候可以使用这个接口。
     *
     * @param arg 字段信息
     * @return affected rows
     */
    public int updateFieldById(ModifyArgument arg){
        ValidationUtils.validArgument(arg, DictionaryConfig.class);
        return sysDicConfigDao.updateFieldById(arg);
    }

    /**
     * 查
     *
     * @param tableName  表名
     * @param columnName 列名
     * @return pojo
     */
    public DictionaryConfig queryById(String schemaName, String tableName, String columnName) {
        DictionaryConfig config = sysDicConfigDao.selectById(schemaName, tableName, columnName);
        if (config == null) {
            throw new BizException("Dictionary is not defined: [" + tableName + "." + columnName + "]");
        } else {
            List<Dictionary> dic = dictionaryService.queryByType(schemaName, tableName, columnName);
            config.setList(dic);
            return config;
        }
    }

    /**
     * 查列表
     *
     * @param params 参数
     * @return list
     */
    public List<DictionaryConfig> queryList(Map<String, Object> params) {
        return sysDicConfigDao.selectList(params);
    }
}
